package com.ops.service.ns.system.beans;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import com.ops.schema.ns.system.OPSRecordAuditInfo;
import com.ops.schema.ns.system.OPSTaxAuthority;
import com.ops.schema.ns.system.SearchTaxAuthoritiesRequest;
import com.ops.schema.ns.system.SearchTaxAuthoritiesResponse;
import com.ops.schema.ns.system.UpdateTaxAuthoritiesRequest;
import com.ops.schema.ns.system.UpdateTaxAuthoritiesResponse;
import com.ops.service.ns.system.client.OnlinePayrollSystemServiceClient;
import com.ops.service.ns.system.exception.ServiceException;

public class TaxAuthoritiesMBean {
	
    private String taxAuth;
    private String taxType; 
    private String rateType; 
    private String ruleGroup;
    private String actionFrom;
    private String actionTo;
    private String mode;
	private String delflag;
	private String saveStatus;
	private List<OPSTaxAuthority> opsTaxlst;
	private OPSTaxAuthority opsTax;
	private Map<String, OPSTaxAuthority>opsTaxMap;
	
	public void searchTaxAuthorities() {
		System.out.println("Inside searchRules");
		SearchTaxAuthoritiesRequest authoritiesRequest = new SearchTaxAuthoritiesRequest();
		authoritiesRequest.setRateType(rateType);
		authoritiesRequest.setRuleGroup(ruleGroup);
		authoritiesRequest.setTaxAuth(taxAuth);
		authoritiesRequest.setTaxType(taxType);
		authoritiesRequest.setActionTo(actionTo);
		authoritiesRequest.setActionFrom(actionFrom);
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		uditInfo.setAction("S");
		uditInfo.setObjectType(getMenuId());
		authoritiesRequest.setOPSRecordAuditInfo(uditInfo);
		try {
			SearchTaxAuthoritiesResponse authoritiesResponse = OnlinePayrollSystemServiceClient
					.searchTaxAuthorities(authoritiesRequest);
			setOpsTaxlst(authoritiesResponse.getOPSTaxAuthority());

		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}
	
	public void resetTaxAuthorities() {
		setTaxAuth(null);
		setTaxType(null);
		setRateType(null);
		setRuleGroup(null);
		setActionFrom(null);
		setActionTo(null);
	}
	
	public void saveTaxAuthorities(ActionEvent event) {

		UpdateTaxAuthoritiesRequest updateRequest = new UpdateTaxAuthoritiesRequest();
		OPSTaxAuthority opsTaxAuthority = new OPSTaxAuthority();
		opsTaxAuthority.setId(opsTax.getId());
		opsTaxAuthority.setName(opsTax.getName()); 
		opsTaxAuthority.setTaxType(opsTax.getTaxType());
		opsTaxAuthority.setRateType(opsTax.getRateType());
		opsTaxAuthority.setAddress1(opsTax.getAddress1());
		opsTaxAuthority.setAddress2(opsTax.getAddress2());
		opsTaxAuthority.setCity(opsTax.getCity());
		opsTaxAuthority.setState(opsTax.getState());
		opsTaxAuthority.setCountry(opsTax.getCountry());
		opsTaxAuthority.setZip(opsTax.getZip());
		opsTaxAuthority.setRuleGroup(opsTax.getRuleGroup());
		opsTaxAuthority.setActiveFrom(opsTax.getActiveFrom());
		opsTaxAuthority.setActiveTo(opsTax.getActiveTo());
		opsTaxAuthority.setOperationFlag("U");
		OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
		uditInfo.setUpdatedBy(getuserId());
		uditInfo.setUpdatedSession(getSessionId());
		if(opsTaxAuthority.getId() != null)
			uditInfo.setAction("U");
			else
			uditInfo.setAction("I");	
			uditInfo.setObjectType(getMenuId());
			opsTaxAuthority.setOPSRecordAuditInfo(uditInfo);
		updateRequest.getOPSTaxAuthority().add(opsTaxAuthority);
		try {
			UpdateTaxAuthoritiesResponse response = persistData(updateRequest);
			System.out.println("STATUS:"
					+ response.getServiceResult().getStatusMsg());
			setSaveStatus(response.getServiceResult().getStatusMsg());
			searchTaxAuthorities();
		} catch (ServiceException e) {
			e.printStackTrace();
		}

	}
	public void delTaxAuthorities(ActionEvent event) {
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (mode.startsWith("Del_") && delflag != null
					&& delflag.equalsIgnoreCase("Yes")) {
				setDelflag(null);
				UpdateTaxAuthoritiesRequest updateRequest = new UpdateTaxAuthoritiesRequest();
				String key = mode.replace("Del_", "").trim();
				OPSTaxAuthority authority = new OPSTaxAuthority();
				authority.setId(key);
				authority.setOperationFlag("D");
				OPSRecordAuditInfo uditInfo = new OPSRecordAuditInfo();
				uditInfo.setUpdatedBy(getuserId());
				uditInfo.setUpdatedSession(getSessionId());
				uditInfo.setAction("D");	
				uditInfo.setObjectType(getMenuId());
				authority.setOPSRecordAuditInfo(uditInfo);
				updateRequest.getOPSTaxAuthority().add(authority);
				try {
					persistData(updateRequest);
					searchTaxAuthorities();
				} catch (ServiceException e) {
					e.printStackTrace();
				}

			}
		}

	}
	
	public OPSTaxAuthority addTaxAuthorities() {
		setSaveStatus(null);
		if (mode != null && !mode.equalsIgnoreCase("")) {
			if (getOpsTaxlst() != null) {
				opsTaxMap = new HashMap<String, OPSTaxAuthority>();
				for (OPSTaxAuthority authority : getOpsTaxlst()) {
					opsTaxMap.put(authority.getId(), authority);
				}
			}
			if (mode.equalsIgnoreCase("newAdd")) {
				opsTax = new OPSTaxAuthority();
			} else if (mode.startsWith("Edit_")) {
				String key = mode.replace("Edit_", "").trim();
				System.out.println("Key:" + key);
				opsTax = opsTaxMap.get(key);
			}
		}
		System.out.println("Inside addUsers:::" + mode);
		return opsTax;
	}
	
	public UpdateTaxAuthoritiesResponse persistData(
			UpdateTaxAuthoritiesRequest authoritiesRequest) throws ServiceException {
		UpdateTaxAuthoritiesResponse authoritiesResponse = OnlinePayrollSystemServiceClient
				.saveTaxAuthorities(authoritiesRequest);
		return authoritiesResponse;
	}

	private String getSessionId() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext()
				.getSession(false);
		return session.getId();
	}

	private String getuserId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("UserId").toString();
	}
	private String getMenuId() {

		ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
		Map<String, Object> sessionMap = externalContext.getSessionMap();
		return sessionMap.get("MenuId").toString();
	}
	
	public String getTaxAuth() {
		return taxAuth;
	}
	public void setTaxAuth(String taxAuth) {
		this.taxAuth = taxAuth;
	}
	public String getTaxType() {
		return taxType;
	}
	public void setTaxType(String taxType) {
		this.taxType = taxType;
	}
	public String getRateType() {
		return rateType;
	}
	public void setRateType(String rateType) {
		this.rateType = rateType;
	}
	public String getRuleGroup() {
		return ruleGroup;
	}
	public void setRuleGroup(String ruleGroup) {
		this.ruleGroup = ruleGroup;
	}
	public String getActionFrom() {
		return actionFrom;
	}
	public void setActionFrom(String actionFrom) {
		this.actionFrom = actionFrom;
	}
	public String getActionTo() {
		return actionTo;
	}
	public void setActionTo(String actionTo) {
		this.actionTo = actionTo;
	}
	public String getMode() {
		return mode;
	}
	public void setMode(String mode) {
		this.mode = mode;
	}
	public String getDelflag() {
		return delflag;
	}
	public void setDelflag(String delflag) {
		this.delflag = delflag;
	}
	public String getSaveStatus() {
		return saveStatus;
	}
	public void setSaveStatus(String saveStatus) {
		this.saveStatus = saveStatus;
	}
	public List<OPSTaxAuthority> getOpsTaxlst() {
		return opsTaxlst;
	}
	public void setOpsTaxlst(List<OPSTaxAuthority> opsTaxlst) {
		this.opsTaxlst = opsTaxlst;
	}
	public OPSTaxAuthority getOpsTax() {
		return opsTax;
	}
	public void setOpsTax(OPSTaxAuthority opsTax) {
		this.opsTax = opsTax;
	}
	public Map<String, OPSTaxAuthority> getOpsTaxMap() {
		return opsTaxMap;
	}
	public void setOpsTaxMap(Map<String, OPSTaxAuthority> opsTaxMap) {
		this.opsTaxMap = opsTaxMap;
	}
	
}
